//
//  EditInfoController.m
//  A段项目
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 Scott. All rights reserved.
//


#import "EditInfoController.h"
#import "HeaderCellOfInfomation.h"
#import "TextViewForProfession.h"
#import "FifthViewFooterView.h"
#import "Person.h"
#import "FMDBDataHandler.h"
#import <DKNightVersion.h>

@interface EditInfoController ()<UITableViewDelegate, UITableViewDataSource,UIPickerViewDelegate,UIPickerViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate,TextViewProfessionDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSDictionary *dic;
@property (nonatomic, retain) NSArray *arrInfo;
@property (nonatomic, retain) UIView *view1;
@property (nonatomic, retain) UIView *viewHeader;
@property (nonatomic, retain) UIPickerView *pick;

@property (nonatomic, retain) NSIndexPath *indexPath; // 存储点击的 row.

@property (nonatomic, retain) NSArray *arrFirst;  //第二区 的选择器数据数组
@property (nonatomic, retain) NSMutableArray *arrProvince; //第仨区的选择器数组

@property (nonatomic, retain) NSArray *arrThird; //第四区的选择器数组

@property (nonatomic, retain) NSMutableArray *arrCity;

@property (nonatomic, retain) NSArray *allItem;

@property (nonatomic, retain) NSMutableArray *cityForArr;

@property (nonatomic, assign) NSInteger pickRows;

@property (nonatomic, assign) NSInteger pickCompent;


@property (nonatomic, retain) UIImage *imageData;

@property (nonatomic,  copy) NSString *nicheng;

@property (nonatomic, retain) NSIndexPath *sectionOneTag;



@end

@implementation EditInfoController

{
    
    int count;
    
   
}
- (void)dealloc {
    
    
     [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    
    [_view1 release];
    
    [_viewHeader release];
    
    [_pick release];
    
    [_imageData release];
    
    [_tableView release];
    
    [_indexPath release];
    
    [_dic release];
    
    [_arrInfo release];
    
    [_arrFirst release];
    
    [_arrProvince release];
    
    [_arrThird release];
    
    [_arrCity release];
    
    [_allItem release];
    
    [_cityForArr release];
    
    [_sectionOneTag release];
    
    [super dealloc];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationItem.title = @"编辑个人信息";
    
    [self handleData];
  
    [self createTableView];
    
    [self handlePickData];
}

- (void)handleData {
    
    NSArray *array1 = @[@"头像"];
    
    NSArray *array2 = @[@"昵称",@"签名档",@"手机号码"];
    
    NSArray *array3 = @[@"性别",@"年龄",@"星座",@"情感状态"];
    
    NSArray *array4 = @[@"出生地",@"居住地",@"出没地"];
    
    NSArray *array5 = @[@"职业",@"兴趣爱好"];
    
    self.arrInfo = @[array1,array2,array3,array4,array5];
    
}

- (void)handlePickData {
    
    self.arrProvince = [NSMutableArray array];
    
    NSMutableArray *arrayForPro = [NSMutableArray array];
    
    self.arrCity = [NSMutableArray array];
    
    self.cityForArr = [NSMutableArray array];
    
    
     NSArray *array1 = @[@"男",@"女"];
    NSArray *array2 = @[@"保密",@"00后",@"90后",@"80后",@"70后",@"60后"];
    NSArray *array3 = @[@"白羊",@"金牛",@"双子",@"巨蟹",@"狮子",@"处女",@"天秤",@"天蝎",@"射手",@"摩羯",@"水瓶",@"双鱼"];
    NSArray *array4 = @[@"保密",@"单身",@"恋爱中",@"订婚",@"已婚",@"离异"];
    
    self.arrFirst = @[array1,array2,array3,array4];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"plist"];
    
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    
    
    NSArray *arrcomponents = [dic allKeys];
    
    NSArray *sortArray = [arrcomponents sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
        
    }];
    
    for (int i = 0; i < sortArray.count; i ++) {
        
        NSString *index = [sortArray objectAtIndex:i];
        
        NSArray *tmp = [[dic objectForKey: index] allKeys];
        
        [self.arrProvince addObject:[tmp objectAtIndex:0]];
        
        [arrayForPro addObject:[dic objectForKey:index]];
        
    }
   
    for (int i = 0; i < arrayForPro.count; i++) {
        
        NSDictionary *dic = [arrayForPro objectAtIndex:i];
        
        NSString *index = [[dic allKeys]lastObject];
        
        NSDictionary *dic006 = [dic objectForKey:index];
        
      
        [self.arrCity addObject:dic006]; //将省区字典加入到数组.
    
        }
    
    
    
    /**将市区名字加入数组. */
    
     NSDictionary *dicCity = [self.arrCity objectAtIndex:7];

    
    NSArray *arr120 = [dicCity allKeys];
    
    for (int  i = 0; i < arr120.count; i ++) {
        
        
    }
    
    
    for (int i = 0; i < self.arrCity.count; i ++) {
        
        NSMutableArray *arrCityOfKey = [NSMutableArray array];
        
        NSDictionary *dicCity = [self.arrCity objectAtIndex:i];
        
        NSArray *arr = [dicCity allKeys];
        
   
        NSArray *arr10086 = [arr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            
            if ([obj1 integerValue] > [obj2 integerValue]) {
                return (NSComparisonResult)NSOrderedDescending;
            }
            
            if ([obj1 integerValue] < [obj2 integerValue]) {
                return (NSComparisonResult)NSOrderedAscending;
            }
            return (NSComparisonResult)NSOrderedSame;
            
        }];
        
        
        
        for (int i = 0; i < arr.count; i ++) {
            
            NSString *str = [arr10086 objectAtIndex:i];
            
            NSDictionary *dic = [dicCity objectForKey:str];
            
            [arrCityOfKey addObject:[dic allKeys]];
            
        }
        
        [self.cityForArr addObject:arrCityOfKey];
        
    }

 //***/最后一组标题.
    
    self.arrThird = @[@"保密",@"计算机/互联网类",@"通信",@"电子类",@"销售类",@"客服类",@"技术支持类",@"生产/营运类",@"采购/物流类",@"生物/制药类",@"医疗/护理类",@"市场/媒体类",@"建筑/房产类",@"人事/行政类",@"咨询类",@"法律类",@"教育/科研类",@"服务业",@"公务员类",@"翻译类",@"体育类",@"其他类"];
    
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[HeaderCellOfInfomation class] forCellReuseIdentifier:@"pool2"];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 5;
    
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    return [[self.arrInfo objectAtIndex:section]count];
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    

    
    if (indexPath.section == 0) {
        
        HeaderCellOfInfomation *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        
       cell.leftLabel.text = @"头像";
        
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:70 / 256.f green:70 / 256.f blue:52 / 256.f alpha:1]);
        
        cell.dk_tintColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);

        
        NSString *str = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"header.jpg"];
        NSData *data = [NSData dataWithContentsOfFile:str];
        
        
        UIImage *image = [UIImage imageWithData:data];
        
        cell.rightImage.image = image;
        
        return cell;
        
    }
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    
    if (cell == nil) {
        
        cell = [[[UITableViewCell alloc]initWithStyle:1 reuseIdentifier:@"pool1"]autorelease];
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:70 / 256.f green:70 / 256.f blue:70 / 256.f alpha:1]);
        
       
    }
    cell.textLabel.dk_textColorPicker =  DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    
    cell.detailTextLabel.dk_textColorPicker=  DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    
    if (indexPath.section != 0) {
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        
    }else {
        
        cell.imageView.image = [UIImage imageNamed:@"xingxing"];
    }
    
    NSArray *array = [self.arrInfo objectAtIndex:indexPath.section];
    
    cell.textLabel.text= [array objectAtIndex:indexPath.row];
    
    if (indexPath.section == 1) {
        
        NSArray *array = [self.arrInfo objectAtIndex:1];
        
        NSArray *arr = [[FMDBDataHandler shareDataBaseHandler] selectedForNanme:[array objectAtIndex:indexPath.row]];
        
        if (arr.count != 0) {
            
            Person *per = [arr objectAtIndex:0];
            
            
            cell.detailTextLabel.text = per.childName;
        }
        
    
        
        
    }
    
    
    if (indexPath.section == 2) {
        
   
        NSArray *array = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:2];
        
        /**遍历数组.找出 与当前下标相同的 address 的值. */
        
        for (int i = 0; i < array.count; i ++) {
            
            Person *per = array[i];
            
            if ([per.address integerValue] == indexPath.row) {
                
                 cell.detailTextLabel.text = [NSString stringWithString:per.name];
            }
            
            
        }
        
    }
    
    /**cell 的详细信息. */
        
    if (indexPath.section == 4) {
        
        
        /**当等于第一行时*/
        if (indexPath.row == 0) {
            
            NSArray *array = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:4];
            
            if (array.count != 0) {
                
                Person *per = array [0];
                
                cell.detailTextLabel.text = [NSString stringWithString:per.name];
                
            }
           
            
        }
        
       
        
    }
    
    if (indexPath.section == 3) {
        
        NSArray *array = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:3];
        
        for (int i = 0; i < array.count; i ++) {
            
            Person *per = array[i];
            
            /**数据库中的数据与这个相等时,赋值. 找到正确的数据,赋给正确的地方. */
            if ([per.address integerValue] == indexPath.row+5) {
                
                cell.detailTextLabel.text = [[NSString stringWithString:per.name]stringByAppendingString:per.childName];
            }
            
    }
    
        
    }
        
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return 80;
    }else {
        
        return 40;
    }
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    if (indexPath.section == 0) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"请选择文件来源" preferredStyle:0];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"本地相册" style:0 handler:^(UIAlertAction * _Nonnull action) {
            
             [self usePhotoLibrary];
            
        }];
        
        
        [alert addAction:action];
        
        
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:1 handler:nil];
        
        [alert addAction:action2];
        
        
        [self presentViewController:alert animated:YES completion:^{
            
            
            
        }];
        

    }
    
    
    if (indexPath.section == 1) {
        
        self.sectionOneTag = indexPath;  //标记点击了第几行.
        
     UITableViewCell *cell  = [self.tableView cellForRowAtIndexPath:indexPath];
        
        TextViewForProfession *textView = [[TextViewForProfession alloc] init];
        
        NSArray *arr = [self.arrInfo objectAtIndex:indexPath.section];
        
        NSString *str = [arr objectAtIndex:indexPath.row];
        
        textView.titleForThere = [NSString stringWithFormat:@"编辑%@",str];
        
        textView.textField = [[UITextField alloc] init];
        
        textView.textField.text = cell.detailTextLabel.text;
        
        NSLog(@"%@",textView.textField.text);
        
        textView.index = indexPath;
        
        textView.delegate  =self;
        
        [self.navigationController pushViewController:textView animated:YES];
        
        [textView release];
        
        
    }

    self.indexPath = indexPath; //记录当前点击的位置.
    
    if ((indexPath.section > 1&&indexPath.section<4)||(indexPath.section ==4 &&indexPath.row != 1)) {
        
        self.view1 =  [[UIView alloc] initWithFrame:self.view.frame];
        
        self.view1.backgroundColor = [UIColor blackColor];
        
        self.view1.alpha = 0.6;
        
        [self.view addSubview:self.view1];
        
        [_view1 release];
        
        
        /**选择器上方的"确定/取消"按钮所子的视图.*/
        self.viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 290, self.view.frame.size.width, 40)];
        [self.view addSubview:self.viewHeader];
        
       self. viewHeader.backgroundColor = [UIColor lightGrayColor];
        
        UIButton *buttonCancal = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonCancal.frame = CGRectMake(20, 0, 50, 40); //取消按钮的布局.
        
        [self.viewHeader addSubview:buttonCancal];
        [self.viewHeader release];
        
        [buttonCancal setTitle:@"取消" forState: UIControlStateNormal];
        
        [buttonCancal addTarget:self action:@selector(cancalAction:) forControlEvents:UIControlEventTouchUpInside]; //添加点击事件.
        
        /**确定按钮的赋值. */
        UIButton *buttonTure = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonTure.frame = CGRectMake(self.view.frame.size.width - 70, 0, 40, 40);
        
        [self.viewHeader addSubview:buttonTure];
        
        [buttonTure setTitle:@"确定" forState: UIControlStateNormal];
        
        [buttonTure addTarget:self action:@selector(tureAction:) forControlEvents:UIControlEventTouchUpInside];
        
        /**选择器的布局. */
        self.pick = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 250, self.view.frame.size.width, 250)];
        
      
        self.pick.backgroundColor = [UIColor whiteColor];
    
        
        self.pick.delegate = self;
        
        /**当点击第二个区时.*/
        if (indexPath.section == 2) {
            
            NSArray *arr = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:2];
            
            
            /**遍历数组,找出对应的信息,并且赋值*/
            for (int i = 0; i < arr.count; i ++) {
                
                Person *per = arr[i];
                
                /*当前所在行数等于数据库中的对应的数据时. */
                if ([per.address integerValue] == indexPath.row) {
                    
                    [self.pick selectRow:[per.postion integerValue]  inComponent:0 animated:YES];
                    
                }
            
            }
            
        }
        
        /*点击第三个分区时.*/
        if (indexPath.section == 3) {
            
            NSArray *arr = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:3];
            
            /**遍历数组,找共同点. */
            for (Person *per in arr) {
                
                if ([per.address integerValue] == indexPath.row + 5) {
                    
                    /*设置选择器在省份的那个区的当前选择位置. */
                    [self.pick selectRow:[per.postion integerValue] inComponent:0 animated:YES];
                    
                    /**当前选择的省份所包含的市区数组. */
                    NSArray *arr110 = [self.cityForArr objectAtIndex:[per.postion integerValue]];
                    
                    NSMutableArray *arr30 = [NSMutableArray array];
                    
                    
                    /**遍历市区数组..得到一个数组,但是数组里只有一个对象,所以拿出来把它加入到 arr30的数组中. */
                    for (int i = 0; i < arr110.count; i++) {
                    
                    NSArray *arr007 = [arr110 objectAtIndex:i];
                                        
                    [arr30 addObject:arr007[0]];
                                        
                }
                    /**当前选择的市区在 arr30的位置.这个位置就是选择器当前行所处的位置. */
                    NSInteger psotion = [arr30 indexOfObject:per.childName];
                    
            
                    [self.pick reloadComponent:1];
                    
                    [self.pick selectRow:psotion inComponent:1 animated:YES];
                    

                    
                }
            }
            }
            
        
        if (indexPath.section == 4) {
            
            NSArray *array = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:4];
            
            
            /**当数组部位空时走这个方法. */
            if (array.count != 0) {
                
            Person *per = array[0];
                
            
            [self.pick selectRow:[per.postion integerValue]  inComponent:0 animated:YES];
            
            
        
            if (indexPath.row == 0) {
                
                [self.pick selectRow:[per.postion integerValue] inComponent:0 animated:YES];
                
                
            }
            
        }
        }
   
        
        [self.view addSubview:self.pick];
        
        [_pick release];
        
    }
    

}


/**调用相册. */
- (void)usePhotoLibrary {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.delegate = self;
    
    picker.allowsEditing = YES;
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:nil];
    
    [picker release];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
 
      UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    NSData *data = UIImageJPEGRepresentation(image, 1);
    
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"header.jpg"];
    
    [data writeToFile:file atomically:YES];
    

    [[NSNotificationCenter defaultCenter] postNotificationName:@"headerImageChange" object:nil];  //給 fifthViewFooterView 发送消息 告诉它换图片

    [picker dismissViewControllerAnimated:YES completion:nil];
    
    [self.tableView reloadData];
    
}

- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
  NSIndexPath *index = [self.tableView indexPathForSelectedRow];
    
    if (index.section == 3) {
        return 2;
    }
   
    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    
    
     NSIndexPath *index = [self.tableView indexPathForSelectedRow];
    
    if (index.section == 2) {
        
    NSArray *arr =   [self.arrFirst objectAtIndex:index.row];
        
        return arr.count;
        
    }else if (index.section == 4) {
        
        return self.arrThird.count;
    }
    
    
    if (component == 0) {
        
        return self.arrProvince.count;
        
        
    }else {
        
        NSInteger count1 = [self.pick selectedRowInComponent:0];
        
        NSArray *arr110 = [self.cityForArr objectAtIndex:count1];
        
        return arr110.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
     NSIndexPath *index = [self.tableView indexPathForSelectedRow];
    
   
  
    
    if (index.section == 2) {
        
      NSArray *arr = [self.arrFirst objectAtIndex:index.row];
        
        return arr[row];
    }else if (index.section == 4&&index.row ==0) {
    
        return self.arrThird[row];
        
    }
    
    if (component == 1) {
        
        NSInteger count1 = [self.pick selectedRowInComponent:0];
        
        NSArray *arr110 = [self.cityForArr objectAtIndex:count1];
        
     NSArray *arr119 =  [arr110 objectAtIndex:row];

        return arr119[0];
        
    }
    

    return self.arrProvince[row];
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
   
  NSIndexPath *index = [self.tableView indexPathForSelectedRow];
    
    if (index.section == 3) {
        
        [self.pick reloadComponent:1];
        
    }
    
}


- (void)viewWillAppear:(BOOL)animated {
    
    
    self.view.dk_backgroundColorPicker = DKColorWithColors([UIColor blackColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    self.pick.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    

    
    self.pick.dk_tintColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);

    
    self.tabBarController.tabBar.hidden = YES;
    
}

/**点击确定取消的方法. */

- (void)cancalAction:(UIButton *)button {
    
    [self.view1 removeFromSuperview];
    
    [self.viewHeader removeFromSuperview];
    
    [self.pick removeFromSuperview];
    
}

- (void)tureAction:(UIButton *)button {
    
    if (self.indexPath.section == 3) {
        
        self.pickCompent = [self.pick selectedRowInComponent:1];
        self.pickRows = [self.pick selectedRowInComponent:0];
        
        /**获取市区信息. */
        NSString *str110 = [self.arrProvince objectAtIndex:self.pickRows];
        
                NSInteger count1 = [self.pick selectedRowInComponent:0];
        
                NSArray *arr110 = [self.cityForArr objectAtIndex:count1];
    
                NSArray *arr119 =  [arr110 objectAtIndex:self.pickCompent];
        
    
       Person *per = [[Person alloc] init];
        
        per.number = 3;
        
        per.name = [NSString stringWithString:str110];
        
        per.childName = [NSString stringWithString:arr119[0]];
        
        per.address = [NSString stringWithFormat:@"%ld",self.indexPath.row + 5];
        
        per.postion = [NSString stringWithFormat:@"%ld",self.pickRows];
        
        /**查询数据库数据. */
        NSArray *array = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:3];
        NSMutableArray *arr = [NSMutableArray array];
       
        for (Person *per in array) {
            
            
            NSString *address =[NSString stringWithFormat:@"%@",per.address];
            
            [arr addObject:address];
            
            
        }
        
        if (array.count < [self.tableView numberOfRowsInSection:3] && ![arr containsObject:per.address]) {
            
            [[FMDBDataHandler shareDataBaseHandler] insertInfo:per];
            
        }else {
            
            
            [[FMDBDataHandler shareDataBaseHandler]updateInfo:per :per.address ];
            
            
        }
        
        
        }
    
    self.pickRows = [self.pick selectedRowInComponent:0];
    
   /**将保存的信息存入数组. */
    if (self.indexPath.section == 2) {
        
     NSArray  *arr = [self.arrFirst objectAtIndex:self.indexPath.row]; /**找出数组. */
        
        NSString *str = [arr objectAtIndex:self.pickRows];
        
        Person *per = [[Person alloc] init];
        
        per.number = 2;
        
        per.name = [NSString stringWithString:str];
        
        per.address = [NSString stringWithFormat:@"%ld",self.indexPath.row];
        
    
        per.postion = [NSString stringWithFormat:@"%ld",self.pickRows];
        
        NSArray *array = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:2];   /**查询在本分区在数据库中的所有数据. */
        
        NSMutableArray *array1 = [NSMutableArray array]; // 用来装 per.address 的数组
        
        /**遍历数组.*/
        for (Person *per in array) {
            
            NSString *address = [NSString stringWithString:per.address];
            
            [array1 addObject:address];
            
        }
        
        /**判断是否需要在数据库中插入信息. */
        
        /**数据库数据的个数小于此分区中的行数,并且数据库中,没有不存在此行的数据时,插入数据. 否则就更新数据库中此行的信息. */
        if (array.count < [self.tableView numberOfRowsInSection:self.indexPath.section]&& ![array1 containsObject:per.address]) {
            
            [[FMDBDataHandler shareDataBaseHandler] insertInfo:per];
            
        }else {
            
            [[FMDBDataHandler shareDataBaseHandler] updateInfo:per :per.address];
        
        }
        
        [per release]; //释放.
       
        
    }
    
    if (self.indexPath.section == 4) {
        
       NSString *str = [self.arrThird objectAtIndex:self.pickRows];
        
        Person *per = [[Person alloc] init];
        
        per.number = 4;
        
        per.name = [NSString stringWithString:str];
        
        per.address = [NSString stringWithFormat:@"%d",4];
        
        per.postion = [NSString stringWithFormat:@"%ld",self.pickRows];
        
        
        NSArray *array = [[FMDBDataHandler shareDataBaseHandler]selectedInfo:4];
        
        if (array.count == 0) {
            
            [[FMDBDataHandler shareDataBaseHandler]insertInfo:per];
            
        }else {
            
            [[FMDBDataHandler shareDataBaseHandler] updateInfo:per :per.address];
            
        }
        
        [per release];
    
    }
    
    
    [self.view1 removeFromSuperview];
    
    [self.viewHeader removeFromSuperview];
    
    [self.pick removeFromSuperview];
  
    [self.tableView reloadData];
    
}

- (void)passedEditedInfo:(NSString *)text :(NSIndexPath *)index {
    

   UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:index];
    
    cell.detailTextLabel.text = [NSString stringWithString:text];
    
    Person *per = [[Person alloc] init];
    
    NSArray *arr = [self.arrInfo objectAtIndex:1];
    
    per.address = [arr objectAtIndex:index.row];
    
    per.childName = text;
    
    per.name = [arr objectAtIndex:index.row];
    
    NSLog(@"%@",per.name);
    
    
   NSArray  *array = [[FMDBDataHandler shareDataBaseHandler] selectedForNanme:per.name];
    
    NSLog(@"*******%ld",array.count);
    
    if (array.count == 0) {
        
        [[FMDBDataHandler shareDataBaseHandler] insertInfo:per];
        
    }else {
    
        NSLog(@"-------");
        
    [[FMDBDataHandler shareDataBaseHandler] updateInfo:per :per.name];
    
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
